package com.pdformparent;

// RadioWithInputView.java实现具体自定义视图示例
import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.dengpp.pdform.base.BaseCustomFormView;

import java.util.HashMap;
import java.util.Map;

public class RadioWithInputView extends BaseCustomFormView {
    private RadioGroup radioGroup;
    private LinearLayout inputContainer;
    private EditText input1, input2, input3;

    private Map<String, Object> currentValue = new HashMap<>();

    @Override
    protected View onCreateView(Context context) {
        LinearLayout rootLayout = new LinearLayout(context);
        rootLayout.setOrientation(LinearLayout.VERTICAL);

        // 创建单选按钮组
        radioGroup = new RadioGroup(context);
        radioGroup.setOrientation(LinearLayout.VERTICAL);

        RadioButton radioOption1 = new RadioButton(context);
        radioOption1.setText("选项1");
        radioOption1.setTag("option1");

        RadioButton radioOption2 = new RadioButton(context);
        radioOption2.setText("选项2");
        radioOption2.setTag("option2");

        RadioButton radioOption3 = new RadioButton(context);
        radioOption3.setText("选项3");
        radioOption3.setTag("option3");

        radioGroup.addView(radioOption1);
        radioGroup.addView(radioOption2);
        radioGroup.addView(radioOption3);

        // 创建输入框容器
        inputContainer = new LinearLayout(context);
        inputContainer.setOrientation(LinearLayout.VERTICAL);
        inputContainer.setVisibility(View.GONE);

        input1 = new EditText(context);
        input1.setHint("输入框1");

        input2 = new EditText(context);
        input2.setHint("输入框2");

        input3 = new EditText(context);
        input3.setHint("输入框3");

        inputContainer.addView(input1);
        inputContainer.addView(input2);
        inputContainer.addView(input3);

        rootLayout.addView(radioGroup);
        rootLayout.addView(inputContainer);

        // 设置单选按钮监听
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            RadioButton checkedRadio = group.findViewById(checkedId);
            if (checkedRadio != null) {
                String selectedOption = (String) checkedRadio.getTag();
                currentValue.put("selectedOption", selectedOption);

                // 根据选择显示/隐藏输入框
                if ("option3".equals(selectedOption)) {
                    inputContainer.setVisibility(View.VISIBLE);
                } else {
                    inputContainer.setVisibility(View.GONE);
                    currentValue.remove("input1");
                    currentValue.remove("input2");
                    currentValue.remove("input3");
                }

                notifyValueChanged(currentValue);
            }
        });

        // 设置输入框监听
        setupTextWatcher(input1, "input1");
        setupTextWatcher(input2, "input2");
        setupTextWatcher(input3, "input3");

        return rootLayout;
    }

    @Override
    protected void onBindData(Object value) {
        if (value instanceof Map) {
            currentValue = new HashMap<>((Map) value);

            String selectedOption = (String) currentValue.get("selectedOption");
            if (selectedOption != null) {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    RadioButton radio = (RadioButton) radioGroup.getChildAt(i);
                    if (selectedOption.equals(radio.getTag())) {
                        radio.setChecked(true);
                        break;
                    }
                }
            }

            input1.setText((String) currentValue.get("input1"));
            input2.setText((String) currentValue.get("input2"));
            input3.setText((String) currentValue.get("input3"));
        }
    }

    @Override
    public Object getCurrentValue() {
        return currentValue;
    }

    private void setupTextWatcher(EditText editText, String key) {
        editText.addTextChangedListener(new android.text.TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(android.text.Editable s) {
                currentValue.put(key, s.toString());
                notifyValueChanged(currentValue);
            }
        });
    }
}